#include "mainwindow.h"

MainWindow::MainWindow()
{
	setWindowTitle(tr("EvPacker"));
	mainWidget=new MainWidget(tr("SPEC"),this);
	mainWidget->setFeatures(QDockWidget::DockWidgetMovable|QDockWidget::DockWidgetClosable);
	outputWidget=new OutputWidget(tr("Output"),this);
	outputWidget->setFeatures(QDockWidget::DockWidgetMovable|QDockWidget::DockWidgetClosable);
	setCentralWidget(mainWidget);
	addDockWidget(Qt::RightDockWidgetArea,outputWidget);
	
	pathDialog=new PathDialog(this);
	
	init_actions();
	init_toolBars();
	init_statusBar();
	init_filepath();
}

void MainWindow::init_actions()
{
	generateAct=new QAction(tr("Generate"),this);
	saveAct=new QAction(tr("Save as"),this);
	buildAct=new QAction(tr("Build"),this);
	connect(generateAct,SIGNAL(triggered()),this,SLOT(generate()));
	connect(saveAct,SIGNAL(triggered()),this,SLOT(save()));
	connect(buildAct,SIGNAL(triggered()),this,SLOT(build()));
}

void MainWindow::init_toolBars()
{
	toolBar=addToolBar(tr("Tool Bar"));
	toolBar->addAction(generateAct);
	toolBar->addAction(saveAct);
	toolBar->addAction(buildAct);
}

void MainWindow::init_statusBar()
{
	statusBar()->showMessage(tr("Ready"));
}

void MainWindow::init_filepath()
{
	pathDialog->show();
	pathDialog->raise();
	pathDialog->activateWindow();
	connect(pathDialog,SIGNAL(path(const QString&, const QString& )),this,SLOT(setPath(const QString&, const QString& )));
}

void MainWindow::generate()
{
	outputWidget->setText(mainWidget->commit());
}

void MainWindow::save()
{
	QFile file;
	QString str;

	str=QFileDialog::getSaveFileName(this, tr("Save File"),
					QDir::homePath()+"/untitled.spec",
					tr("RPM Specfile: (*.spec)"));
	if(!str.isEmpty())
	{
		file.setFileName(str);
		if (!file.open(QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Text))
		{
			QMessageBox::critical ( this, tr("Error!"), tr("Can't open file :")+file.fileName(),
				QMessageBox::Yes);
			return;
		}
		QTextStream out(&file);
		out<<outputWidget->getText();
		file.close();
	}
}

void MainWindow::build()
{
	QFile file;
	QString strfile;
	
	file.setFileName(konsole);
	if(!file.exists())
	{
		int rtv;
		rtv=QMessageBox::critical (this, tr("Error!"),tr("Can't open file :")+konsole+"\n"+
			tr("Do you want to configure it now ?"),
			QMessageBox::Yes|QMessageBox::No);
		if(rtv==QMessageBox::Yes)
		{
			pathDialog->show();
			pathDialog->raise();
			pathDialog->activateWindow();
		}
		return;
	}
	
	file.setFileName(rpmbuild);
	if(!file.exists())
	{
		int rtv;
		rtv=QMessageBox::critical (this, tr("Error!"),tr("Can't open file :")+rpmbuild+"\n"+
			tr("Do you want to configure it now ?"),
			QMessageBox::Yes|QMessageBox::No);
		if(rtv==QMessageBox::Yes)
		{
			pathDialog->show();
			pathDialog->raise();
			pathDialog->activateWindow();
		}
		return;
	}
	
	strfile=QFileDialog::getOpenFileName(this, tr("Open File"),
					QDir::homePath()+"/untitled.spec",
					tr("RPM Specfile: (*.spec)"));
	if(!strfile.isEmpty())
	{
		file.setFileName(strfile);
		if(!file.exists())
		{
			QMessageBox::critical ( this, tr("Error!"), tr("Can't open file :")+strfile,
				QMessageBox::Yes);
			return;
		}
	}
	QStringList args;
	args<<"--noclose"<<"-e"<<rpmbuild<<"-ba"<<strfile;
	
	QProcess::startDetached(konsole,args);
}

void MainWindow::setPath(const QString& konsole, const QString& rpmbuild)
{
	this->konsole=konsole;
	this->rpmbuild=rpmbuild;
}

PathDialog::PathDialog( QWidget * parent , Qt::WindowFlags f )
	:QDialog( parent, f )
{
	setWindowTitle(tr("Configure"));
	init_layout();
	init_connect();
}

void PathDialog::init_layout()
{
	vlayout=new QVBoxLayout(this);
	box=new QGroupBox(tr("Programs"),this);
	glayout=new QGridLayout(box);
	
	Konsole=new QLabel(tr("&konsole"),box);
	Rpmbuild=new QLabel(tr("&rpmbuild"),box);
	konsole=new QLineEdit("/usr/bin/konsole",box);
	rpmbuild=new QLineEdit("/usr/bin/rpmbuild",box);
	Konsole->setBuddy(konsole);
	Rpmbuild->setBuddy(rpmbuild);
	
	glayout->addWidget(Konsole,0,0);
	glayout->addWidget(konsole,0,1);
	glayout->addWidget(Rpmbuild,1,0);
	glayout->addWidget(rpmbuild,1,1);
	box->setLayout(glayout);
	
	bbox=new QDialogButtonBox(QDialogButtonBox::Ok,Qt::Horizontal,this);
	
	vlayout->addWidget(box);
	vlayout->addWidget(bbox);
	setLayout(vlayout);
}

void PathDialog::init_connect()
{
	connect(bbox,SIGNAL(accepted ()),this,SLOT(sendPath()));
	connect(bbox,SIGNAL(accepted ()),this,SLOT(close()));
}

void PathDialog::sendPath()
{
	path(konsole->text(),rpmbuild->text());
}
